//
//  HorizontalScrollView.swift
//  SmallParts
//
//  Created by 航汇聚 on 2019/10/8.
//  Copyright © 2019 BellaZhou. All rights reserved.
//

//
//  当滑动到第三个的时候,接下来要返回第一个，index的计算不变，但是可以改变contentOffset和第二个以及第三个的显示图
//

import UIKit

class HorizontalScrollView: UIView {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var scrollContentView: UIStackView!
    @IBOutlet weak var pageControl: UIPageControl!
    var timer: Timer?
    let duration = 1.5
    var indexOfPage = 0
    var bannerMsgObjs: [BannerMsg]?
    
    open class func loadLoopView() -> HorizontalScrollView {
        let loopView = Bundle.main.loadNibNamed("HorizontalScrollView", owner: self, options: nil)?.first as! HorizontalScrollView
        return loopView
    }
    func autoScrollByDuration(_ duration: TimeInterval) {
        timer = Timer.scheduledTimer(withTimeInterval: duration, repeats: true) { (timer) in
            let offsetHeight = self.bounds.size.height
            let offsetWidth = self.bounds.size.width
            let countOfScrollContentSubViews = self.scrollContentView.subviews.count
            var offsetPoint = CGPoint(x: CGFloat(Float(offsetWidth)*Float(self.indexOfPage)), y: 0)
            if self.indexOfPage > countOfScrollContentSubViews-1 {
                self.indexOfPage = 0
                self.scrollView.contentOffset = CGPoint(x: -offsetWidth/5, y: 0)
                offsetPoint = CGPoint(x: 0, y: 0)
            }
            self.scrollView.scrollRectToVisible(CGRect(x: offsetPoint.x, y: offsetPoint.y, width: offsetWidth, height: offsetHeight), animated: true)
            self.pageControl.currentPage = self.indexOfPage
            self.indexOfPage += 1
        }
        timer!.fire()
    }
    func invalidTimer() {
        timer?.invalidate()
    }
}
extension HorizontalScrollView: UIScrollViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        timer?.invalidate()
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.indexOfPage = Int(scrollView.contentOffset.x/scrollView.bounds.size.width)
        self.pageControl.currentPage = self.indexOfPage
        self.autoScrollByDuration(duration)
    }
}

extension HorizontalScrollView {
    func setImageForLoop() {
        var index = 0
        for imgView in self.scrollContentView.subviews {
           let imgView = imgView as! UIImageView
            DispatchQueue.main.async {
                if index < self.bannerMsgObjs!.count {
                    let imgURL = self.bannerMsgObjs?[index].bannerImgUrl
                    if (imgURL != nil) {
                        let downloadTask = URLSession.shared.dataTask(with: imgURL!) { (data, response, error) in
                            DispatchQueue.main.async {
                                imgView.image = UIImage(data: data!)
                            }
                        }
                        downloadTask.resume()
                    }
                }
                index += 1
            }
        }
    }
}
